package com.zhibang.config;

import com.zhibang.util.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)
    public Result ex(Exception ex){
        ex.printStackTrace();
        return Result.fail(ex.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result ex(AccessDeniedException ex){
        return Result.fail(401,"请先登录");
    }

    // 处理认证异常
    @ExceptionHandler(AuthenticationException.class)
    public void authenticationException(AuthenticationException ex) {
        // 如果发生了认证异常，本类不处理并抛出，交给security配置的认证异常处理器处理
        throw ex;
    }
}
